import { Toast } from 'vant'
import {interfaceUtils} from './interface.js'
let crypto = require('crypto');
// https://www.npmjs.com/package/jsencrypt?activeTab=versions
import JsEncrypt from "jsencrypt";
// const {JSEncrypt} = require('encryptlong')

class Address extends interfaceUtils {
    static IsRequire=8
    static Name="address"
    static TimeStamp=new Date().getTime()
    constructor(name,phone,address,provinceId,cityId,zoneId,consumerId,addressId,type,doType ){
        super()
        this.name=name
        this.phone=phone==""?"null":phone 
        this.address=address
        this.provinceId=provinceId
        this.cityId =cityId 
        this.zoneId =zoneId 
        this.consumerId =consumerId     
        this.addressId = addressId ||'null'
        this.type=type
        this.timeStamp=new Date().getTime()
        /**
         *  type  1 2 3
         *  家 公司  学校
         */
        this.type=type
       
    }
    _getParams(){
        const publicKey=Address.publicKey
        let rsa = new JsEncrypt();
        rsa.setPublicKey(publicKey);
        const { name,phone,consumerId,addressId } =this    
      
        const params=JSON.stringify( { name,phone,consumerId,addressId
        }) 
        //加密
        const result= rsa.encrypt(params)      
         //    https://www.cnblogs.com/aeolian/p/12133598.html
        //  https://www.jianshu.com/p/19583c7e67dd
        return encodeURIComponent(result) 
      
       
     }
     _getOther(){      
        const { provinceId,cityId,zoneId,address,type } =this    
        // console.log("other",JSON.stringify( { provinceId,cityId,zoneId,address,type }))
        return JSON.stringify( { provinceId,cityId,zoneId,address,type })
     }

     _getSign(){
        const key =Address.md5Key+""
        const {phone} =this 
        const consumerFrom=Address.consumerFrom+""
        const {timeStamp}=this
     //    https://blog.csdn.net/wail521/article/details/96430873       
        //  const sign  = this._parmaSort({ key ,phone,consumerFrom ,timeStamp  }) 
        const sign  = phone +consumerFrom+timeStamp+key
         const result = crypto.createHash('md5').update(sign).digest("hex")    
         return result 
     }
 
     _getData(){
       
         const params=this._getParams()
         const sign=this._getSign()
         const other=this._getOther()
         const {phone} = this
         const consumerFrom=Address.consumerFrom
         const {timeStamp}=this
 
         let formData = new FormData();
         formData.append("params",params);
         formData.append("sign",sign);
         formData.append("other",other);
         formData.append("phone",phone);
         formData.append("consumerFrom",consumerFrom);
         formData.append("timeStamp",timeStamp);
 
         return  formData
 
 
     }
 
       /**
      * 
      * @param {keyObj} 将对象排序 变成 字符串
      *  例如 a=1&b=2 
      */
     _parmaSort(keyObj){
         let keyArr = [];
       for (let key in keyObj) {
         keyArr.push(key);
       }
         let reaKryArr = keyArr.sort();
         let newArrStr = "";
         for (let i = 0; i < keyArr.length; i++) {
           newArrStr += reaKryArr[i] + "=" + keyObj[reaKryArr[i]] + "&";
         }
     
         let str = newArrStr.substr(0, newArrStr.length - 1);
         return str
     }
}

export function qualified(data){
    if(data == null || data ==""||data == undefined ){
        return false
    }else{
        return true
    }
}

export function validata(data,class_type){
    
    let qualify =false

    let set= Object.entries(data).slice(0,class_type.IsRequire)
    let ret= set.find((value,index)=>{
        return !qualified( value[1])
    })
    if( ret == undefined){  
       
        //符合要求   
        // name,phone,address,provinceId,cityId,zoneId,consumerId,addressId
        
        switch (class_type.Name) {
                case 'address':
                    if( qualified(data.name)  &&  qualified(data.phone) && qualified(data.address)  &&  qualified(data.provinceId) &&
                        qualified(data.cityId)  &&  qualified(data.zoneId) && qualified(data.consumerId)  &&  qualified(data.addressId)  ){  


                            if((data.phone+"").length<9 || (data.phone+"").length>11){
                                Toast.fail({
                                    message:"请输入9-11位手机号"
                                })
                                qualify=false
                            }else{
                           
                                qualify=true 
                            }

                      
                                  
                    }else{             
                        qualify=false                    
                    }                    
                        break;               
        }        
     
        return qualify    
    }else{
       Toast.fail(ret[0] +" is Null");
        return false   
    }   
 


    
}


export { Address }